package cn.boy.gui.adminstrator;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import cn.boy.adapter.HistoryAdapter;
import cn.boy.adapter.StudentAdapter;
import cn.boy.domain.Student;
import cn.boy.gui.student.StudentInfoActivity;
import cn.boy.home.HomeApplication;
import cn.boy.home.R;
import cn.boy.service.HomeService;

public class DormitoryMemberActivity extends Activity implements OnClickListener{
	
	protected static final String TAG = "DormitoryMemberActivity";
	EditText txtSearch;
	Button searchButton;
	ListView listView;
	ProgressDialog pd;

	String retValue = null;
	List<Student> students;
	
	HomeApplication app ;
	
	StudentAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dormitory_member);
		
		findViews();
		
		students = new ArrayList<Student>();
		
		app = (HomeApplication) getApplicationContext();
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Student student = (Student) parent.getItemAtPosition(position);
//				Toast.makeText(DormitoryMemberActivity.this, student.getName(), 1).show();
				Intent intent = new Intent(DormitoryMemberActivity.this,StudentInfoActivity.class);
				app.setStudent(student);
				app.setFlag(false);
				startActivity(intent);
				
			}
		});
		
	}

	private void findViews() {
		txtSearch = (EditText)findViewById(R.id.et_search);
		searchButton = (Button)findViewById(R.id.btn_search);
		searchButton.setOnClickListener(this);
		listView = (ListView)findViewById(R.id.lv_dorm_member);
		
	}

	public void onClick(View v) {
		
		final String no = txtSearch.getText().toString().trim();
		if(!TextUtils.isEmpty(no)){
			showDialog();
			new Thread(new Runnable() {
				public void run() {
					try {
						retValue = HomeService.searchDormitoryMember(no);
						if(!retValue.equals("")){
							sendHandlerMessage(1);
						}else{
							sendHandlerMessage(2);
						}
					} catch (Exception e) {
						Log.e(TAG, e.toString());
						sendHandlerMessage(0);
					}
				}
			}).start();
			
		}else{
			Toast.makeText(DormitoryMemberActivity.this, R.string.search_no_empty, 1).show();
		}
	}
	private void showDialog() {
		pd = new ProgressDialog(DormitoryMemberActivity.this); 
		pd.setMessage(getResources().getString(R.string.get_dorms_ing));
		pd.show();
	}
	
	private void sendHandlerMessage(int what) {
		Message msg = handler.obtainMessage(what);
		handler.sendMessage(msg);
		
	}
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(pd!=null){
				pd.dismiss();
			}
			switch(msg.what){
			case 0:
				Toast.makeText(DormitoryMemberActivity.this, R.string.net_unreacgabke, 1).show();
				break;
			case 1:
				try {
					students.clear();
					JSONArray array = new JSONArray(retValue);
					for(int i = 0;i < array.length();i++){
						JSONObject obj = array.getJSONObject(i);
						Student student  = mappingEntity(obj);
						students.add(student);
					}
					if(adapter == null){
						adapter = new StudentAdapter(DormitoryMemberActivity.this, students);
						listView.setAdapter(adapter);
					}else{
						adapter.setStudents(students);
						adapter.notifyDataSetChanged();
					}
					
				} catch (JSONException e) {
					Log.e(TAG, e.toString());
				}
				break;
			case 2:
				Toast.makeText(DormitoryMemberActivity.this, R.string.get_data_fail, 1).show();
				break;
			}
		}
		

		private Student mappingEntity(JSONObject obj) {
			try {
				String no = obj.getString("no");
				String name = obj.getString("name");
				String sex = obj.getString("sex");
				String age = obj.getString("age");
				String dormitoryNO = obj.getString("dormitoryNO");
				String classInfo = obj.getString("classInfo");
				return new Student(no, name, "", sex, age, dormitoryNO, classInfo);
			} catch (JSONException e) {
				Log.e(TAG, e.toString());
				return null;
			}
			
		}

		
	};
}
